﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tron.Helpers;
using Tron.Animations;

namespace Tron
{
    class BoardGraphics
    {
        BlockGraphics[,] blockGraphicsArray;
        public BlockGraphics[,] BlockGraphicsArray
        {
            get
            {
                return blockGraphicsArray;
            }
            set
            {
                blockGraphicsArray = value;
            }
        }

        public List<BlockGraphics> PlayerBlockGraphics{get;set;}
        public List<BlockAnimator> PlayerBlockAnimators{get;set;}

        Matrix boardTransform;
        public Matrix BoardTransform
        {
            get
            {
                return BoardTransform;
            }
        }

        int blockSizeInPixels;

        public BoardGraphics(Game game, int rows, int columns)
        {
            Texture2D solidTexture = game.Content.Load<Texture2D>("Textures\\Blocks\\solid");
            blockSizeInPixels = solidTexture.Width; // Assume square blocks

            int viewportWidth = game.GraphicsDevice.Viewport.Width;
            int viewportHeight = game.GraphicsDevice.Viewport.Height;
            Vector2 bottomRight = new Vector2(columns * blockSizeInPixels, rows * blockSizeInPixels);
            if (bottomRight.Y > viewportHeight)
                throw new ArgumentException("Too many rows.");
            if (bottomRight.X > viewportWidth)
                throw new ArgumentException("Too many columns.");

            float verticalGapSize = viewportHeight - bottomRight.Y;
            float verticalOffset = verticalGapSize / 2;
            float horizontalOffset = viewportWidth - bottomRight.X - verticalOffset;

            boardTransform = 
                //Matrix.CreateScale(blockSizeInPixels) *
                Matrix.CreateTranslation(horizontalOffset, verticalOffset, 0);

            blockGraphicsArray = new BlockGraphics[columns, rows];

            Color blockColor = Color.DarkGreen;
            Vector3 blockColorVariation = new Vector3(0.05f);

            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < columns; column++)
                {
                    Vector2 position = new Vector2(column, row);
                    position *= blockSizeInPixels;

                    Vector3 variation = RandomAid.NextVector3InRange(-blockColorVariation, blockColorVariation);
                    Vector3 variateBlockColorVector = Vector3.Clamp(variation + blockColor.ToVector3(),
                        Vector3.Zero, Vector3.One);
                    
                    Color variateBlockColor = new Color(variateBlockColorVector);

                    blockGraphicsArray[column, row] = new BlockGraphics(game, position, variateBlockColor);
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.None, boardTransform);
            for (int row = 0; row < blockGraphicsArray.GetLength(1); row++)
            {
                for (int column = 0; column < blockGraphicsArray.GetLength(0); column++)
                {
                    blockGraphicsArray[column, row].Draw(spriteBatch);
                }
            }
            for(int i = 0; i < PlayerBlockGraphics.Count; i++)
            {
                PlayerBlockAnimators[i].Update(gameTime);
                PlayerBlockGraphics[i].Position = PlayerBlockAnimators[i].Position * blockSizeInPixels;
                PlayerBlockGraphics[i].Draw(spriteBatch);
            }
            spriteBatch.End();
        }
    }
}
